package fa.wangxing.leaf.Controller;


import fa.wangxing.leaf.Service.OrderService;
import fa.wangxing.leaf.pojo.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @RequestMapping("/createorder")
    public String createOrder(HttpSession session, ModelMap m) {
        User user = (User) session.getAttribute("session_user");
        List<Cart> cartList = orderService.getcart(user.getId());
        double price = 0;
        for(Cart cart : cartList){
            price += cart.getPrice();
        }
        BigDecimal b = new BigDecimal(price);
        price = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        Order order = new Order();
        order.setOrder_host(user.getId());
        order.setOrder_price(price);
        orderService.createorder(order);
        int orderid = orderService.getnewestorder(user.getId());
        System.out.println(orderid);
        List<Orderitems> items = new ArrayList<>();
        for(int flag=0;flag<cartList.size();flag++){
            Orderitems item = new Orderitems();
            item.setItems_order(orderid);
            item.setItems_dishId(cartList.get(flag).getId());
            item.setItems_prices(cartList.get(flag).getPrice());
            items.add(item);
        }
        for(Orderitems item : items){
            orderService.addItem(item);
        }
        orderService.cleancart(user.getId());
        return "main";
    }

    @RequestMapping("/showorder")
    public String showOrder(HttpSession session, ModelMap m) {
        if(session.getAttribute("session_user") == null){
            m.put("msg","未登录，请登录");
            return "redirect:/user/tologin";
        }
        User user = (User) session.getAttribute("session_user");
        List<Order> orderList = orderService.getorder(user.getId());
        for(Order order : orderList){
            System.out.println(order);
        }
        m.put("orderList", orderList);
        return "showorder";
    }

    @RequestMapping("/showdetail/{id}")
    public String showDetail(ModelMap m, @PathVariable int id) {
        Order order = orderService.getOrderbyid(id);
        System.out.println(order);
        List<Dishes> dishesList = orderService.getdishes(id);
        m.put("dishesList", dishesList);
        m.put("order", order);
        return "showdetail";
    }

    @RequestMapping("/cancle/{id}")
    public String cancel(ModelMap m, @PathVariable int id,HttpSession session) {
        orderService.cancleorder(id);
        return showOrder(session,m);
    }

    @RequestMapping("/pay/{id}")
    public String pay(ModelMap m, @PathVariable int id,HttpSession session) {
        orderService.payorder(id);
        return showOrder(session,m);
    }

}
